﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace 医院药库管理系统
{
    public partial class 药品信息维护 : Form
    {
        public DataGridViewComboBoxColumn typeColumn;
        public DataGridViewComboBoxColumn PhysicalstateColumn;
        public DataGridViewComboBoxColumn DosageFormColumn;
        public 药品信息维护()
        {
            InitializeComponent();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void lb_cj_Click(object sender, EventArgs e)
        {

        }

        private void tb_yptj_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlCommand updateCommand = new SqlCommand();
            updateCommand.Connection = sqlConnection;
            updateCommand.CommandText =
                "UPDATE tb_Medicine"
                + " SET No=@NewNo,Name=@Name,Acronym=@Acronym,Made_Date=@Made_Date,Valid_Date=@Valid_Date,InDate=@InDate,FirmName=@FirmName,PurchasePrice=@PurchasePrice,price=@price,Quantity=@Quantity,Type=@Type,PhysicalState=@PhysicalState,DosageForm=@DosageForm,in_principal=@in_principal"
                + " WHERE No=@OldNo;";
            updateCommand.Parameters.Add("@NewNo", SqlDbType.Int, 0, "No");
            updateCommand.Parameters.Add("@Name", SqlDbType.VarChar,0, "Name");
            updateCommand.Parameters.Add("@Acronym", SqlDbType.VarChar,0, "Acronym");
            updateCommand.Parameters.Add("@Made_Date", SqlDbType.Date, 100, "Made_Date");
            updateCommand.Parameters.Add("@Valid_Date", SqlDbType.Date, 100, "Valid_Date");
            updateCommand.Parameters.Add("@InDate", SqlDbType.Date,100, "InDate");
            updateCommand.Parameters.Add("@FirmName", SqlDbType.VarChar,0, "FirmName");
            updateCommand.Parameters.Add("@PurchasePrice", SqlDbType.Money, 100, "PurchasePrice");
            updateCommand.Parameters.Add("@price", SqlDbType.Money,100, "price");
            updateCommand.Parameters.Add("@Quantity", SqlDbType.Int, 10, "Quantity");
            updateCommand.Parameters.Add("@Type", SqlDbType.Int,10, "Type");
            updateCommand.Parameters.Add("@PhysicalState", SqlDbType.Int,10, "PhysicalState");
            updateCommand.Parameters.Add("@DosageForm", SqlDbType.Int,10,"DosageForm");
            updateCommand.Parameters.Add("@in_principal", SqlDbType.VarChar,0,"in_principal");
           
            updateCommand.Parameters.Add("@OldNo", SqlDbType.Int, 0, "No");
           
            updateCommand.Parameters["@OldNo"].SourceVersion = DataRowVersion.Original;
          
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.UpdateCommand = updateCommand;
            
            DataTable csTable1 = (DataTable)this.dgv_ypwh.DataSource;
            sqlConnection.Open();
            int rowAffected = sqlDataAdapter.Update(csTable1);
            sqlConnection.Close();
            MessageBox.Show("更新" + rowAffected.ToString() + "行。");
             
        }

        private void 药品信息维护_Load(object sender, EventArgs e)
        {
           
        }

        private void bt_zr_Click(object sender, EventArgs e)
        {
            this.dgv_ypwh.DataSource ="";
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = "SELECT * FROM tb_Medicine;";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;
            DataTable csTable = new DataTable();
            sqlConnection.Open();
            sqlDataAdapter.Fill(csTable);
            sqlConnection.Close();
            this.dgv_ypwh.DataSource = csTable;
            SqlCommand sqlCommand1= new SqlCommand();
            sqlCommand1.Connection = sqlConnection;
            sqlCommand1.CommandText = "SELECT * FROM tb_Type;";
            SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter();
            sqlDataAdapter1.SelectCommand = sqlCommand1;
            DataTable Type= new DataTable();
            sqlConnection.Open();
            sqlDataAdapter1.Fill(Type);
            sqlConnection.Close();
          

            DataGridViewComboBoxColumn typeColumn = new DataGridViewComboBoxColumn();                  //声明并实例化数据网格视图下拉框列，用于设置先修课程；
            typeColumn.Name = "TypeName";                                                             //设置下拉框列的名称；
            typeColumn.HeaderText = "药品类型";                                                        //设置下拉框列的表头文本；
            typeColumn.DataSource = Type;                                               //设置下拉框列的数据源为先修课程数据表；
            typeColumn.DisplayMember = "TypeName";                                                         //设置下拉框列的显示成员为（先修课程数据表的）名称（列）；
            typeColumn.ValueMember = "No";                                                             //设置下拉框列的值成员为（先修课程数据表的）编号（列）；
            typeColumn.DataPropertyName = "Type";                                               //设置下拉框列的数据属性名称为（课程数据表的）先修课程编号（列）；
            typeColumn.DisplayIndex = 11;                                                               //设置下拉框列的显示顺序；
            typeColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;                             //设置下拉框列的自动调整列宽模式为填充；
            this.dgv_ypwh.Columns.Add(typeColumn);

            SqlCommand sqlCommand2 = new SqlCommand();
            sqlCommand2.Connection = sqlConnection;
            sqlCommand2.CommandText = "SELECT * FROM tb_Physicalstate;";
            SqlDataAdapter sqlDataAdapter2 = new SqlDataAdapter();
            sqlDataAdapter2.SelectCommand = sqlCommand2;
            DataTable Physicalstate = new DataTable();
            sqlConnection.Open();
            sqlDataAdapter2.Fill(Physicalstate);
            sqlConnection.Close();
           

            DataGridViewComboBoxColumn PhysicalstateColumn = new DataGridViewComboBoxColumn();                  //声明并实例化数据网格视图下拉框列，用于设置先修课程；
            PhysicalstateColumn.Name = "PhysicalstateName";                                                             //设置下拉框列的名称；
            PhysicalstateColumn.HeaderText = "物理状态";                                                        //设置下拉框列的表头文本；
            PhysicalstateColumn.DataSource = Physicalstate;                                               //设置下拉框列的数据源为先修课程数据表；
            PhysicalstateColumn.DisplayMember = "PhysicalstateName";                                                         //设置下拉框列的显示成员为（先修课程数据表的）名称（列）；
            PhysicalstateColumn.ValueMember = "No";                                                             //设置下拉框列的值成员为（先修课程数据表的）编号（列）；
            PhysicalstateColumn.DataPropertyName = "Physicalstate";                                               //设置下拉框列的数据属性名称为（课程数据表的）先修课程编号（列）；
            PhysicalstateColumn.DisplayIndex = 13;                                                               //设置下拉框列的显示顺序；
            PhysicalstateColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;                             //设置下拉框列的自动调整列宽模式为填充；
            this.dgv_ypwh.Columns.Add(PhysicalstateColumn);

            SqlCommand sqlCommand3 = new SqlCommand();
            sqlCommand3.Connection = sqlConnection;
            sqlCommand3.CommandText = "SELECT * FROM tb_DosageForm;";
            SqlDataAdapter sqlDataAdapter3 = new SqlDataAdapter();
            sqlDataAdapter3.SelectCommand = sqlCommand3;
            DataTable DosageForm= new DataTable();
            sqlConnection.Open();
            sqlDataAdapter3.Fill(DosageForm);
            sqlConnection.Close();
            

            DataGridViewComboBoxColumn DosageFormColumn = new DataGridViewComboBoxColumn();                  //声明并实例化数据网格视图下拉框列，用于设置先修课程；
            DosageFormColumn.Name = "PhysicalstateName";                                                             //设置下拉框列的名称；
            DosageFormColumn.HeaderText = "剂型";                                                        //设置下拉框列的表头文本；
            DosageFormColumn.DataSource = DosageForm;                                               //设置下拉框列的数据源为先修课程数据表；
            DosageFormColumn.DisplayMember = "Name";                                                         //设置下拉框列的显示成员为（先修课程数据表的）名称（列）；
            DosageFormColumn.ValueMember = "No";                                                             //设置下拉框列的值成员为（先修课程数据表的）编号（列）；
            DosageFormColumn.DataPropertyName = "DosageForm";                                               //设置下拉框列的数据属性名称为（课程数据表的）先修课程编号（列）；
            DosageFormColumn.DisplayIndex = 15;                                                               //设置下拉框列的显示顺序；
            DosageFormColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;                             //设置下拉框列的自动调整列宽模式为填充；
            this.dgv_ypwh.Columns.Add(DosageFormColumn);
            this.dgv_ypwh.Columns["Type"].Visible = false;
            this.dgv_ypwh.Columns["PhysicalState"].Visible = false;
            this.dgv_ypwh.Columns["DosageForm"].Visible = false;
            this.dgv_ypwh.Columns["No"].HeaderText = "编号";
            this.dgv_ypwh.Columns["Name"].HeaderText = "名称";
            this.dgv_ypwh.Columns["Made_Date"].HeaderText = "生产日期";
            this.dgv_ypwh.Columns["Valid_Date"].HeaderText = "有效期至";
            this.dgv_ypwh.Columns["InDate"].HeaderText = "入库时间";
            this.dgv_ypwh.Columns["InDate"].ReadOnly = true;
            this.dgv_ypwh.Columns["FirmName"].HeaderText = "生产厂商";
            this.dgv_ypwh.Columns["PurchasePrice"].HeaderText = "药品进价";
            this.dgv_ypwh.Columns["Price"].HeaderText = "零售价";
            this.dgv_ypwh.Columns["in_principal"].HeaderText = "负责人";
            this.dgv_ypwh.Columns["Quantity"].HeaderText = "数量";
            this.dgv_ypwh.Columns["Acronym"].HeaderText = "首字母缩写";
        }   
    }
}
